package com.qipay.admin.statement.controller;

import com.qipay.admin.BaseController;
import com.qipay.admin.statement.service.StatementService;
import com.qipay.excel.service.ExcelUtils;
import com.qipay.security.UserDetailsServiceImpl;
import com.qipay.user.entity.UserLevel;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

/**
 * Creator: Yao
 * Date:    2017/9/21
 * For:
 * Other:
 */
@Controller
@RequestMapping("/statement")
public class StatementController extends BaseController {

    @Autowired
    StatementService statementService;
    @Autowired
    ExcelUtils excelUtils;


    @RequestMapping(value = {"/", ""}, method = RequestMethod.GET)
    public ModelAndView basic(
            @AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
            @RequestParam(value = "cpId", required = false, defaultValue = "") Long cpId,
            @RequestParam(value = "from", required = false, defaultValue = "") String from,
            @RequestParam(value = "to", required = false, defaultValue = "") String to) throws ParseException {

        // check
        Assert.notNull(loginUser);

        // init
        ModelAndView view = createModelAndView("statement");

        // fill
        statementService.fillView(view, loginUser, cpId, from, to);

        return view;
    }

    @RequestMapping(value = {"/order"}, method = RequestMethod.GET)
    public ModelAndView order(
            @AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
            @RequestParam(value = "cpId", required = false, defaultValue = "") Long cpId,
            @RequestParam(value = "channel", required = false) Integer channel,
            @RequestParam(value = "from", required = false, defaultValue = "") String from,
            @RequestParam(value = "to", required = false, defaultValue = "") String to) throws ParseException {

        // check
        Assert.notNull(loginUser);
        if (loginUser.getLevel() == UserLevel.CP) {
            cpId = null == cpId ? Long.valueOf(loginUser.getUsername()) : cpId;
        }
        Assert.notNull(cpId);

        // init
        ModelAndView view = createModelAndView("statement-order");

        // fill
        statementService.fillOrderView(view, cpId, from, to, channel);

        return view;
    }

    @RequestMapping(value = {"/order/export"}, method = RequestMethod.GET)
    public void orderExport(
            @AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
            @RequestParam(value = "cpId", required = false, defaultValue = "") Long cpId,
            @RequestParam(value = "channel", required = false) Integer channel,
            @RequestParam(value = "from", required = false, defaultValue = "") String from,
            @RequestParam(value = "to", required = false, defaultValue = "") String to,
            HttpServletResponse resp) throws ParseException, IOException {

        // check
        Assert.notNull(loginUser);
        if (loginUser.getLevel() == UserLevel.CP) {
            cpId = null == cpId ? Long.valueOf(loginUser.getUsername()) : cpId;
        }
        Assert.notNull(cpId);

        // export
        excelUtils.download(resp, statementService.ExportOrderView(cpId, from, to, channel));
    }

    @RequestMapping(value = {"/all"}, method = RequestMethod.GET)
    public ModelAndView all(
            @AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
            @RequestParam(value = "cpId", required = false, defaultValue = "") Long cpId,
            @RequestParam(value = "channel", required = false) Integer channel,
            @RequestParam(value = "cpName", required = false, defaultValue = "") String cpName,
            @RequestParam(value = "agentId", required = false, defaultValue = "") Long agentId,
            @RequestParam(value = "contract", required = false, defaultValue = "") String contract,
            @RequestParam(value = "from", required = false, defaultValue = "") String from,
            @RequestParam(value = "to", required = false, defaultValue = "") String to) throws ParseException {
        // check
        Assert.notNull(loginUser);

        // init
        ModelAndView view = createModelAndView("statement-all");
        agentId = loginUser.getLevel() == UserLevel.AGENT ? Long.valueOf(loginUser.getUsername()) : agentId;
        view.addObject("level", loginUser.getLevel());

        // fill
        statementService.fillAllView(view, from, to, cpId, cpName, agentId, contract, channel);

        return view;
    }

    @RequestMapping(value = {"/all/export"}, method = RequestMethod.GET)
    public void allExport(
            @AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
            @RequestParam(value = "cpId", required = false, defaultValue = "") Long cpId,
            @RequestParam(value = "channel", required = false) Integer channel,
            @RequestParam(value = "cpName", required = false, defaultValue = "") String cpName,
            @RequestParam(value = "agentId", required = false, defaultValue = "") Long agentId,
            @RequestParam(value = "contract", required = false, defaultValue = "") String contract,
            @RequestParam(value = "from", required = false, defaultValue = "") String from,
            @RequestParam(value = "to", required = false, defaultValue = "") String to,
            HttpServletResponse resp) throws ParseException, IOException {

        // check
        Assert.notNull(loginUser);

        // init
        ModelAndView view = createModelAndView("statement-all");
        agentId = loginUser.getLevel() == UserLevel.AGENT ? Long.valueOf(loginUser.getUsername()) : agentId;
        view.addObject("level", loginUser.getLevel());

        // export
        excelUtils.download(resp, statementService.ExportAllView(loginUser, from, to, cpId, cpName, agentId, contract, channel));
    }

    @RequestMapping(value = {"/sp"}, method = RequestMethod.GET)
    public ModelAndView sp(
            @AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
            @RequestParam(value = "channel", required = false) Integer channel,
            @RequestParam(value = "spId", required = false, defaultValue = "") Long spId,
            @RequestParam(value = "spName", required = false, defaultValue = "") String spName,
            @RequestParam(value = "service", required = false, defaultValue = "") String service,
            @RequestParam(value = "from", required = false, defaultValue = "") String from,
            @RequestParam(value = "to", required = false, defaultValue = "") String to) throws ParseException {

        // check
        Assert.notNull(loginUser);

        // init
        ModelAndView view = createModelAndView("statement-sp");

        // fill
        statementService.fillSpView(view, from, to, spId, spName, service, channel);

        return view;
    }

    @RequestMapping(value = {"/sp/export"}, method = RequestMethod.GET)
    public void spExport(
            @RequestParam(value = "channel", required = false) Integer channel,
            @RequestParam(value = "spId", required = false, defaultValue = "") Long spId,
            @RequestParam(value = "spName", required = false, defaultValue = "") String spName,
            @RequestParam(value = "service", required = false, defaultValue = "") String service,
            @RequestParam(value = "from", required = false, defaultValue = "") String from,
            @RequestParam(value = "to", required = false, defaultValue = "") String to,
            HttpServletResponse resp) throws IOException, ParseException {

        excelUtils.download(resp, statementService.ExportSpView(from, to, spId, spName, service, channel));
    }
}
